        <!DOCTYPE html>
        <html>
        <head>
                <meta charset="utf-8">
        <title>TimeOfImpactSolver class / box2d_console Library / Dart Documentation</title>
        <link rel="stylesheet" type="text/css"
            href="../styles.css">
        <link href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800" rel="stylesheet" type="text/css">
        <link rel="shortcut icon" href="../favicon.ico">
        
        </head>
        <body data-library="box2d_console" data-type="TimeOfImpactSolver">
        <div class="page">
        <div class="header">
          <a href="../index.html"><div class="logo"></div></a>
          <a href="../index.html">Dart Documentation</a>
         &rsaquo; <a href="../box2d_console.html">box2d_console</a> &rsaquo; <a href="../box2d_console/TimeOfImpactSolver.html">TimeOfImpactSolver</a>        <div id="search-box">
          <input type="search" name="q" id="q" autocomplete="off"
              class="search-input" placeholder="Search API">
        </div>
        
      </div>
      <div class="drop-down" id="drop-down"></div>
      
        <div class="nav">
        
</div>
<div class="content">
        <h2><strong>TimeOfImpactSolver</strong>
          class
        </h2>
        
<button id="show-inherited" class="show-inherited">Hide inherited</button>
<div class="doc">
<p>This is a pure position solver for a single movable body in contact with
multiple non-moving bodies.</p>
<pre class="source">
class TimeOfImpactSolver {
  List&lt;TimeOfImpactConstraint&gt; constraints;
  int count;
  Body toiBody;

  /** Pooling. */
  final TimeOfImpactSolverManifold psm;
  final Vector rA;
  final Vector rB;
  final Vector P;
  final Vector temp;

  TimeOfImpactSolver() :
    count = 0,
    toiBody = null,
    constraints = new List&lt;TimeOfImpactConstraint&gt;(4),

    // Initialize pool variables.
    psm = new TimeOfImpactSolverManifold(),
    rA = new Vector(),
    rB = new Vector(),
    P = new Vector(),
    temp = new Vector() {
    for (int i = 0; i&lt;constraints.length; i++){
      constraints[i] = new TimeOfImpactConstraint();
    }
  }

  void initialize(List&lt;Contact&gt; contacts, int argCount, Body argToiBody) {
    count = argCount;
    toiBody = argToiBody;

    if(count &gt;= constraints.length){
      List&lt;TimeOfImpactConstraint&gt; old = constraints;
      int newLen = Math.max(count, old.length*2);
      constraints = new List&lt;TimeOfImpactConstraint&gt;(newLen);
      constraints.setRange(0, old.length, old);
      for(int i=old.length; i&lt;constraints.length; i++){
        constraints[i] = new TimeOfImpactConstraint();
      }
    }

    for (int i=0; i&lt;count; i++) {
      Contact contact = contacts[i];

      Fixture fixtureA = contact.fixtureA;
      Fixture fixtureB = contact.fixtureB;
      Shape shapeA = fixtureA.shape;
      Shape shapeB = fixtureB.shape;
      num radiusA = shapeA.radius;
      num radiusB = shapeB.radius;
      Body bodyA = fixtureA.body;
      Body bodyB = fixtureB.body;
      Manifold manifold = contact.manifold;

      assert(manifold.pointCount &gt; 0);

      TimeOfImpactConstraint constraint = constraints[i];
      constraint.bodyA = bodyA;
      constraint.bodyB = bodyB;
      constraint.localNormal.setFrom(manifold.localNormal);
      constraint.localPoint.setFrom(manifold.localPoint);
      constraint.type = manifold.type;
      constraint.pointCount = manifold.pointCount;
      constraint.radius = radiusA + radiusB;

      for (int j = 0; j &lt; constraint.pointCount; ++j){
        ManifoldPoint cp = manifold.points[j];
        constraint.localPoints[j] = cp.localPoint;
      }
    }
  }

  /**
   * Perform one solver iteration. Returns true if converged.
   */
  bool solve(num baumgarte){
    num minSeparation = 0;

    for (int i = 0; i &lt; count; ++i){
      TimeOfImpactConstraint c = constraints[i];
      Body bodyA = c.bodyA;
      Body bodyB = c.bodyB;

      num massA = bodyA.mass;
      num massB = bodyB.mass;

      // Only the TimeOfImpact body should move.
      if (bodyA == toiBody){
        massB = 0.0;
      } else{
        massA = 0.0;
      }

      num invMassA = massA * bodyA.invMass;
      num invIA = massA * bodyA.invInertia;
      num invMassB = massB * bodyB.invMass;
      num invIB = massB * bodyB.invInertia;

      // Solve normal constraints
      for (int j = 0; j &lt; c.pointCount; ++j){
        psm.initialize(c, j);
        Vector normal = psm.normal;

        Vector point = psm.point;
        num separation = psm.separation;

        rA.setFrom(point).subLocal(bodyA.sweep.center);
        rB.setFrom(point).subLocal(bodyB.sweep.center);

        // Track max constraint error.
        minSeparation = Math.min(minSeparation, separation);

        // Prevent large corrections and allow slop.
        num C = MathBox.clamp(baumgarte * (separation +
            Settings.LINEAR_SLOP), -Settings.MAX_LINEAR_CORRECTION, 0.0);

        // Compute the effective mass.
        num rnA = Vector.crossVectors(rA, normal);
        num rnB = Vector.crossVectors(rB, normal);
        num K = invMassA + invMassB + invIA * rnA * rnA + invIB * rnB * rnB;

        // Compute normal impulse
        num impulse = K &gt; 0.0 ? - C / K : 0.0;

        P.setFrom(normal).mulLocal(impulse);

        temp.setFrom(P).mulLocal(invMassA);
        bodyA.sweep.center.subLocal(temp);
        bodyA.sweep.angle -= invIA * Vector.crossVectors(rA, P);
        bodyA.synchronizeTransform();

        temp.setFrom(P).mulLocal(invMassB);
        bodyB.sweep.center.addLocal(temp);
        bodyB.sweep.angle += invIB * Vector.crossVectors(rB, P);
        bodyB.synchronizeTransform();
      }
    }

    // We can't expect minSpeparation &gt;= -_LINEAR_SLOP because we don't
    // push the separation above -_LINEAR_SLOP.
    return minSeparation &gt;= -1.5 * Settings.LINEAR_SLOP;
  }
}
</pre>
</div>
<div>
<h3>Constructors</h3>
<div class="method"><h4 id="TimeOfImpactSolver">
<button class="show-code">Code</button>
new <strong>TimeOfImpactSolver</strong>() <a class="anchor-link" href="#TimeOfImpactSolver"
              title="Permalink to TimeOfImpactSolver.TimeOfImpactSolver">#</a></h4>
<div class="doc">
<pre class="source">
TimeOfImpactSolver() :
  count = 0,
  toiBody = null,
  constraints = new List&lt;TimeOfImpactConstraint&gt;(4),

  // Initialize pool variables.
  psm = new TimeOfImpactSolverManifold(),
  rA = new Vector(),
  rB = new Vector(),
  P = new Vector(),
  temp = new Vector() {
  for (int i = 0; i&lt;constraints.length; i++){
    constraints[i] = new TimeOfImpactConstraint();
  }
}
</pre>
</div>
</div>
</div>
<div>
<h3>Properties</h3>
<div class="field"><h4 id="constraints">
<button class="show-code">Code</button>
List&lt;<a href="../box2d_console/TimeOfImpactConstraint.html">TimeOfImpactConstraint</a>&gt;         <strong>constraints</strong> <a class="anchor-link"
            href="#constraints"
            title="Permalink to TimeOfImpactSolver.constraints">#</a>
        </h4>
        <div class="doc">
<pre class="source">
List&lt;TimeOfImpactConstraint&gt; constraints;
</pre>
</div>
</div>
<div class="field"><h4 id="count">
<button class="show-code">Code</button>
int         <strong>count</strong> <a class="anchor-link"
            href="#count"
            title="Permalink to TimeOfImpactSolver.count">#</a>
        </h4>
        <div class="doc">
<pre class="source">
int count;
</pre>
</div>
</div>
<div class="field"><h4 id="P">
<button class="show-code">Code</button>
final <a href="../box2d_console/Vector.html">Vector</a>         <strong>P</strong> <a class="anchor-link"
            href="#P"
            title="Permalink to TimeOfImpactSolver.P">#</a>
        </h4>
        <div class="doc">
<pre class="source">
final Vector P;
</pre>
</div>
</div>
<div class="field"><h4 id="psm">
<button class="show-code">Code</button>
final <a href="../box2d_console/TimeOfImpactSolverManifold.html">TimeOfImpactSolverManifold</a>         <strong>psm</strong> <a class="anchor-link"
            href="#psm"
            title="Permalink to TimeOfImpactSolver.psm">#</a>
        </h4>
        <div class="doc">
<p>Pooling.</p>
<pre class="source">
final TimeOfImpactSolverManifold psm;
</pre>
</div>
</div>
<div class="field"><h4 id="rA">
<button class="show-code">Code</button>
final <a href="../box2d_console/Vector.html">Vector</a>         <strong>rA</strong> <a class="anchor-link"
            href="#rA"
            title="Permalink to TimeOfImpactSolver.rA">#</a>
        </h4>
        <div class="doc">
<pre class="source">
final Vector rA;
</pre>
</div>
</div>
<div class="field"><h4 id="rB">
<button class="show-code">Code</button>
final <a href="../box2d_console/Vector.html">Vector</a>         <strong>rB</strong> <a class="anchor-link"
            href="#rB"
            title="Permalink to TimeOfImpactSolver.rB">#</a>
        </h4>
        <div class="doc">
<pre class="source">
final Vector rB;
</pre>
</div>
</div>
<div class="field inherited"><h4 id="runtimeType">
<button class="show-code">Code</button>
final Type         <strong>runtimeType</strong> <a class="anchor-link"
            href="#runtimeType"
            title="Permalink to TimeOfImpactSolver.runtimeType">#</a>
        </h4>
        <div class="inherited-from">inherited from Object </div><div class="doc">
<p>A representation of the runtime type of the object.</p>
<pre class="source">
external Type get runtimeType;
</pre>
</div>
</div>
<div class="field"><h4 id="temp">
<button class="show-code">Code</button>
final <a href="../box2d_console/Vector.html">Vector</a>         <strong>temp</strong> <a class="anchor-link"
            href="#temp"
            title="Permalink to TimeOfImpactSolver.temp">#</a>
        </h4>
        <div class="doc">
<pre class="source">
final Vector temp;
</pre>
</div>
</div>
<div class="field"><h4 id="toiBody">
<button class="show-code">Code</button>
<a href="../box2d_console/Body.html">Body</a>         <strong>toiBody</strong> <a class="anchor-link"
            href="#toiBody"
            title="Permalink to TimeOfImpactSolver.toiBody">#</a>
        </h4>
        <div class="doc">
<pre class="source">
Body toiBody;
</pre>
</div>
</div>
</div>
<div>
<h3>Operators</h3>
<div class="method inherited"><h4 id="==">
<button class="show-code">Code</button>
bool <strong>operator ==</strong>(other) <a class="anchor-link" href="#=="
              title="Permalink to TimeOfImpactSolver.operator ==">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>The equality operator.</p>
<p>The default behavior for all <code>Object</code>s is to return true if and
only if <code>this</code> and 
<span class="param">other</span> are the same object.</p>
<p>If a subclass overrides the equality operator it should override
the <code>hashCode</code> method as well to maintain consistency.</p>
<pre class="source">
bool operator ==(other) =&gt; identical(this, other);
</pre>
</div>
</div>
</div>
<div>
<h3>Methods</h3>
<div class="method inherited"><h4 id="hashCode">
<button class="show-code">Code</button>
int <strong>hashCode</strong>() <a class="anchor-link" href="#hashCode"
              title="Permalink to TimeOfImpactSolver.hashCode">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Get a hash code for this object.</p>
<p>All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator <code>==</code>. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.</p>
<p>If a subclass overrides <code>hashCode</code> it should override the
equality operator as well to maintain consistency.</p>
<pre class="source">
external int hashCode();
</pre>
</div>
</div>
<div class="method"><h4 id="initialize">
<button class="show-code">Code</button>
void <strong>initialize</strong>(List&lt;<a href="../box2d_console/Contact.html">Contact</a>&gt; contacts, int argCount, <a href="../box2d_console/Body.html">Body</a> argToiBody) <a class="anchor-link" href="#initialize"
              title="Permalink to TimeOfImpactSolver.initialize">#</a></h4>
<div class="doc">
<pre class="source">
void initialize(List&lt;Contact&gt; contacts, int argCount, Body argToiBody) {
  count = argCount;
  toiBody = argToiBody;

  if(count &gt;= constraints.length){
    List&lt;TimeOfImpactConstraint&gt; old = constraints;
    int newLen = Math.max(count, old.length*2);
    constraints = new List&lt;TimeOfImpactConstraint&gt;(newLen);
    constraints.setRange(0, old.length, old);
    for(int i=old.length; i&lt;constraints.length; i++){
      constraints[i] = new TimeOfImpactConstraint();
    }
  }

  for (int i=0; i&lt;count; i++) {
    Contact contact = contacts[i];

    Fixture fixtureA = contact.fixtureA;
    Fixture fixtureB = contact.fixtureB;
    Shape shapeA = fixtureA.shape;
    Shape shapeB = fixtureB.shape;
    num radiusA = shapeA.radius;
    num radiusB = shapeB.radius;
    Body bodyA = fixtureA.body;
    Body bodyB = fixtureB.body;
    Manifold manifold = contact.manifold;

    assert(manifold.pointCount &gt; 0);

    TimeOfImpactConstraint constraint = constraints[i];
    constraint.bodyA = bodyA;
    constraint.bodyB = bodyB;
    constraint.localNormal.setFrom(manifold.localNormal);
    constraint.localPoint.setFrom(manifold.localPoint);
    constraint.type = manifold.type;
    constraint.pointCount = manifold.pointCount;
    constraint.radius = radiusA + radiusB;

    for (int j = 0; j &lt; constraint.pointCount; ++j){
      ManifoldPoint cp = manifold.points[j];
      constraint.localPoints[j] = cp.localPoint;
    }
  }
}
</pre>
</div>
</div>
<div class="method inherited"><h4 id="noSuchMethod">
<button class="show-code">Code</button>
<strong>noSuchMethod</strong>(String name, List args) <a class="anchor-link" href="#noSuchMethod"
              title="Permalink to TimeOfImpactSolver.noSuchMethod">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p><code>noSuchMethod</code> is invoked when users invoke a non-existant method
on an object. The name of the method and the arguments of the
invocation are passed to <code>noSuchMethod</code>. If <code>noSuchMethod</code>
returns a value, that value becomes the result of the original
invocation.</p>
<p>The default behavior of <code>noSuchMethod</code> is to throw a
<code>noSuchMethodError</code>.</p>
<pre class="source">
external Dynamic noSuchMethod(String name, List args);
</pre>
</div>
</div>
<div class="method inherited"><h4 id="Object">
<button class="show-code">Code</button>
const <strong>Object</strong>() <a class="anchor-link" href="#Object"
              title="Permalink to TimeOfImpactSolver.Object">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Creates a new <code>Object</code> instance.</p>
<p><code>Object</code> instances have no meaningful state, and are only useful
through their identity. An <code>Object</code> instance is equal to itself
only.</p>
<pre class="source">
const Object();
</pre>
</div>
</div>
<div class="method"><h4 id="solve">
<button class="show-code">Code</button>
bool <strong>solve</strong>(num baumgarte) <a class="anchor-link" href="#solve"
              title="Permalink to TimeOfImpactSolver.solve">#</a></h4>
<div class="doc">
<p>Perform one solver iteration. Returns true if converged.</p>
<pre class="source">
bool solve(num baumgarte){
  num minSeparation = 0;

  for (int i = 0; i &lt; count; ++i){
    TimeOfImpactConstraint c = constraints[i];
    Body bodyA = c.bodyA;
    Body bodyB = c.bodyB;

    num massA = bodyA.mass;
    num massB = bodyB.mass;

    // Only the TimeOfImpact body should move.
    if (bodyA == toiBody){
      massB = 0.0;
    } else{
      massA = 0.0;
    }

    num invMassA = massA * bodyA.invMass;
    num invIA = massA * bodyA.invInertia;
    num invMassB = massB * bodyB.invMass;
    num invIB = massB * bodyB.invInertia;

    // Solve normal constraints
    for (int j = 0; j &lt; c.pointCount; ++j){
      psm.initialize(c, j);
      Vector normal = psm.normal;

      Vector point = psm.point;
      num separation = psm.separation;

      rA.setFrom(point).subLocal(bodyA.sweep.center);
      rB.setFrom(point).subLocal(bodyB.sweep.center);

      // Track max constraint error.
      minSeparation = Math.min(minSeparation, separation);

      // Prevent large corrections and allow slop.
      num C = MathBox.clamp(baumgarte * (separation +
          Settings.LINEAR_SLOP), -Settings.MAX_LINEAR_CORRECTION, 0.0);

      // Compute the effective mass.
      num rnA = Vector.crossVectors(rA, normal);
      num rnB = Vector.crossVectors(rB, normal);
      num K = invMassA + invMassB + invIA * rnA * rnA + invIB * rnB * rnB;

      // Compute normal impulse
      num impulse = K &gt; 0.0 ? - C / K : 0.0;

      P.setFrom(normal).mulLocal(impulse);

      temp.setFrom(P).mulLocal(invMassA);
      bodyA.sweep.center.subLocal(temp);
      bodyA.sweep.angle -= invIA * Vector.crossVectors(rA, P);
      bodyA.synchronizeTransform();

      temp.setFrom(P).mulLocal(invMassB);
      bodyB.sweep.center.addLocal(temp);
      bodyB.sweep.angle += invIB * Vector.crossVectors(rB, P);
      bodyB.synchronizeTransform();
    }
  }

  // We can't expect minSpeparation &gt;= -_LINEAR_SLOP because we don't
  // push the separation above -_LINEAR_SLOP.
  return minSeparation &gt;= -1.5 * Settings.LINEAR_SLOP;
}
</pre>
</div>
</div>
<div class="method"><h4 id="TimeOfImpactSolver">
<button class="show-code">Code</button>
new <strong>TimeOfImpactSolver</strong>() <a class="anchor-link" href="#TimeOfImpactSolver"
              title="Permalink to TimeOfImpactSolver.TimeOfImpactSolver">#</a></h4>
<div class="doc">
<pre class="source">
TimeOfImpactSolver() :
  count = 0,
  toiBody = null,
  constraints = new List&lt;TimeOfImpactConstraint&gt;(4),

  // Initialize pool variables.
  psm = new TimeOfImpactSolverManifold(),
  rA = new Vector(),
  rB = new Vector(),
  P = new Vector(),
  temp = new Vector() {
  for (int i = 0; i&lt;constraints.length; i++){
    constraints[i] = new TimeOfImpactConstraint();
  }
}
</pre>
</div>
</div>
<div class="method inherited"><h4 id="toString">
<button class="show-code">Code</button>
String <strong>toString</strong>() <a class="anchor-link" href="#toString"
              title="Permalink to TimeOfImpactSolver.toString">#</a></h4>
<div class="inherited-from">inherited from Object </div><div class="doc">
<p>Returns a string representation of this object.</p>
<pre class="source">
external String toString();
</pre>
</div>
</div>
</div>
        </div>
        <div class="clear"></div>
        </div>
        
        <div class="footer">
          
        </div>
        <script async src="../client-live-nav.js"></script>
        </body></html>
        
